package com.study.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 *
 */
public class Solution_349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        if (nums1 == null || nums2 == null) {
            return null;
        }
        Set<Integer> nums1Set = new HashSet<>();
        for (int i : nums1) {
            nums1Set.add(i);
        }
        int idx2 = 0;
        Set<Integer> common = new HashSet<>();
        while (idx2 < nums2.length) {
            if (nums1Set.contains(nums2[idx2])) {
                common.add(nums2[idx2]);
            }
            idx2++;
        }
        int[] result = new int[common.size()];
        int idx = 0;
        for (Integer i : common) {
            result[idx++] = i;
        }
        return result;
    }
}
